package kr.co.sdworld.api.apns.repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WhereCondition
{
	private List<Condition> conditionList = new ArrayList<Condition>();
	private List<Order> orderList = new ArrayList<Order>();

	public Map<String, Object> toParamMap()
	{
		List<String> statementList = new ArrayList<String>();
		for ( Condition cond : conditionList)
		{
			StringBuilder statement = new StringBuilder();
			if ( cond.getOper() != null)
			{
				statement.append( cond.getKey()).append( " ").append( cond.getOper().getOperString()).append( " ");

				if ( cond.getValue() instanceof String)
				{
					statement.append( "'").append( cond.getValue()).append( "'");
				}
				else
				{
					statement.append( cond.getValue());
				}
			}
			else
			{
				statement.append( cond.getStatement());
			}
			statementList.add( statement.toString());
		}
		Map<String, Object> param = new HashMap<String, Object>();
		param.put( "whereCondition", statementList);

		List<String> orderList = new ArrayList<String>();
		for ( Order order : this.orderList)
		{
			StringBuilder statement = new StringBuilder();
			if ( order.getColumn() != null && order.getColumn().length() > 0)
			{
				statement.append( order.getColumn()).append( " ").append( order.getOrderType().getType());
				orderList.add( statement.toString());
			}
		}
		param.put( "orderCondition", orderList);

		return param;
	}

	public void addCondition( String key, Object value, Operation operation)
	{
		this.conditionList.add( new Condition( key, value, operation));
	}

	public void addOrder( String column, OrderType orderType)
	{
		this.orderList.add( new Order( column, orderType));
	}

	public enum Operation
	{
		OPT_EQUAL("="), OPT_NOT_EQUAL("!="), OPT_GREATOR_THEN("<"), OPT_GREATOR_EQUAL_THEN("<="), OPT_LESS_THEN(">"), OPT_LESS_EQUAL_THEN(">=");

		Operation(String operString)
		{
			this.operString = operString;
		}

		private String operString;

		public String getOperString()
		{
			return operString;
		}

		public void setOperString( String operString)
		{
			this.operString = operString;
		}
	}

	public enum OrderType
	{
		DESC("DESC"), ASC("ASC");

		private String type;

		OrderType(String type)
		{
			this.type = type;
		}

		public String getType()
		{
			return type;
		}
	}

	public class Condition
	{
		private String key;
		private Operation oper;
		private Object value;
		private String statement;

		public Condition(String key, Object value, Operation oper)
		{
			this.key = key;
			this.value = value;
			this.oper = oper;
		}

		public Condition(String statement)
		{
			this.statement = statement;
		}

		public String getKey()
		{
			return key;
		}

		public void setKey( String key)
		{
			this.key = key;
		}

		public Operation getOper()
		{
			return oper;
		}

		public void setOper( Operation oper)
		{
			this.oper = oper;
		}

		public Object getValue()
		{
			return value;
		}

		public void setValue( Object value)
		{
			this.value = value;
		}

		public String getStatement()
		{
			return this.statement;
		}
	}

	public class Order
	{
		private String column;
		private OrderType orderType;

		public Order(String column, OrderType orderType)
		{
			this.column = column;
			this.orderType = orderType;
		}

		public String getColumn()
		{
			return column;
		}

		public void setColumn( String column)
		{
			this.column = column;
		}

		public OrderType getOrderType()
		{
			return orderType;
		}

		public void setOrderType( OrderType orderType)
		{
			this.orderType = orderType;
		}
	}

	public static void main( String[] args)
	{
		int a = 1;
		Object obj = a;

		System.out.println( obj.getClass());
		System.out.println( int.class == obj.getClass());
	}
}
